package com.uenpay.im.ui.old.activity.attendance;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.utilities.QyxDateUtils;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author SL
 * @created 2016-3-16 下午8:16:42
 * @copyRight http://www.qiyunxin.com
 */
public class AttendanceListAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater;
	private ArrayList<AttendanceEntity> arrayList;
	private Context mContext;

	public AttendanceListAdapter(Context _Context,
			ArrayList<AttendanceEntity> _ArrayList) {
		mLayoutInflater = LayoutInflater.from(_Context);
		arrayList = _ArrayList;
		mContext = _Context;
	}

	public void setData(ArrayList<AttendanceEntity> _ArrayList){
		arrayList = _ArrayList;
		notifyDataSetChanged();
	}

	public void addData(ArrayList<AttendanceEntity> _ArrayList){
		arrayList.addAll(_ArrayList);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList == null ? null : arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		AttendanceViewHodler mAttendanceViewHodler = null;
		AttendanceEntity mAttendanceEntity = arrayList.get(arg0);
		if (arg1 == null) {
			arg1 = mLayoutInflater.inflate(R.layout.item_attendance_layout,
					arg2,false);
			mAttendanceViewHodler = new AttendanceViewHodler();
			mAttendanceViewHodler.avatarIv = (RoundImageView) arg1
					.findViewById(R.id.avatarIv);
			mAttendanceViewHodler.name = (TextView) arg1
					.findViewById(R.id.nameTv);
			mAttendanceViewHodler.startDate = (TextView) arg1
					.findViewById(R.id.startDate);
			mAttendanceViewHodler.endDate = (TextView) arg1
					.findViewById(R.id.endDate);
			arg1.setTag(mAttendanceViewHodler);
		} else {
			mAttendanceViewHodler = (AttendanceViewHodler) arg1.getTag();
		}
		mAttendanceViewHodler.avatarIv.SetUrl(APIConfiguration
				.getAvatarUrlNormal(mAttendanceEntity.custid,
						PacketType.PERSONAL));
		mAttendanceViewHodler.name.setText(IMApplication.config.getUserName());
		if (mAttendanceEntity.checkintime != 0) {
			long msg_time = mAttendanceEntity.checkintime * 1000;
			mAttendanceViewHodler.startDate.setText(QyxDateUtils
					.generateDateTimeString(new Date(msg_time)));
			mAttendanceViewHodler.startDate.setTextColor(mContext
					.getResources().getColor(R.color.CLR8C8C8C));
		} else {
			mAttendanceViewHodler.startDate.setText(R.string.un_sign_in);
			mAttendanceViewHodler.startDate.setTextColor(mContext
					.getResources().getColor(R.color.the_color_red));
		}

		if (mAttendanceEntity.checkouttime != 0) {
			long msg_time = mAttendanceEntity.checkouttime * 1000;
			mAttendanceViewHodler.endDate.setText(QyxDateUtils
					.generateDateTimeString(new Date(msg_time)));
			mAttendanceViewHodler.endDate.setTextColor(mContext.getResources()
					.getColor(R.color.CLR8C8C8C));
		} else {
			mAttendanceViewHodler.endDate.setText(R.string.un_sign_out);
			mAttendanceViewHodler.endDate.setTextColor(mContext.getResources()
					.getColor(R.color.the_color_red));
		}
		return arg1;
	}

	private class AttendanceViewHodler {
		private RoundImageView avatarIv;
		private TextView name, startDate, endDate;
	}

}
